package com.hfjyz.call.dao;

import java.util.List;

import com.hfjyz.base.dao.BaseDao;
import com.hfjyz.base.entity.CallEntity;
import com.hfjyz.base.util.BaseUtil;

public abstract class CallDao extends BaseDao<CallEntity> {

    public CallDao() {
        super(CallEntity.class);
    }

    @SuppressWarnings("unchecked")
    public long countCall(String targetEntity, String alias, String whereClause, List<Object> paramList) {
        String hql = "select count(call.id) from CallEntity call, " + targetEntity;
        Object[] values = null;
        if (!BaseUtil.isEmpty(whereClause)) {
            hql += whereClause;
            hql += " and call.callType=?";
            paramList.add(getType());
            values = paramList.toArray();
        } else {
            hql += " where call.callType=?";
            values = new Object[] { getType() };
        }
        hql += " and call.targetId=" + alias + ".id";
        List<Object> result = hibernateTemplate.find(hql, values);

        return (Long) result.get(0);
    }

    @SuppressWarnings("unchecked")
    public long countCallByTargetAndName(String callName, Integer targetId) {
        String hql = "select count(call.id) from CallEntity call where call.callName=? and call.targetId=? and call.callType=?";
        Object[] values = new Object[] { callName, targetId, getType() };
        List<Object> result = hibernateTemplate.find(hql, values);

        return (Long) result.get(0);
    }

    protected abstract int getType();
}
